package com.heima.behavior.service.impl;

import com.alibaba.fastjson.JSON;
import com.heima.behavior.service.BehaviorService;
import com.heima.common.constants.BehaviorConstants;
import com.heima.common.constants.KafkaConstants;
import com.heima.common.redis.CacheService;
import com.heima.model.behavior.dtos.LikesBehaviorDto;
import com.heima.model.behavior.dtos.LikesOrUnlike;
import com.heima.model.behavior.dtos.ReadBehaviorDto;
import com.heima.model.behavior.dtos.UnLikesBehaviorDto;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.kafka.MessageDto;
import com.heima.utils.common.BaseContext;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.stereotype.Service;

/**
 * @author 熏橙橙
 * @version 1.0
 * @date 2023-08-02 11:38
 */
@Service
@Slf4j
public class BehaviorServiceImpl implements BehaviorService {

    @Autowired
    private CacheService cacheService;
    @Autowired
    private KafkaTemplate<String,String> kafkaTemplate;

    /**
     * 用户点赞
     *
     * @param dto
     * @return
     */
    @Override
    public ResponseResult likes(LikesBehaviorDto dto) {
        //校验参数
        if (dto==null||dto.getOperation()==null||dto.getType()==null||dto.getArticleId()==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //判断是否登录
        LikesOrUnlike l = new LikesOrUnlike();
        BeanUtils.copyProperties(dto,l);
        if (BaseContext.getCurrentId()==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.NEED_LOGIN);
        }
        l.setUserId(BaseContext.getCurrentId());
        //创建key
        String key = BehaviorConstants.LIKES+l.getUserId()+"_"+dto.getArticleId();
        //判断是点赞还是取消点赞
        if (dto.getOperation()==0){
            cacheService.set(key, JSON.toJSONString(l));
        }else {
            cacheService.delete(key);
        }
        //逻辑处理,分发kafka消息,点赞数量实时更新
        MessageDto messageDto = MessageDto.builder()
                .apArticleId(dto.getArticleId().toString())
                .type(1)
                .count(dto.getOperation() == 0 ? 1 : -1)
                .build();
        kafkaTemplate.send(KafkaConstants.MSG,JSON.toJSONString(messageDto));

        return ResponseResult.okResult(200,dto.getOperation()==0?"点赞成功":"取消点赞成功");
    }

    /**
     * 用户阅读文章
     *
     * @param dto
     * @return
     */
    @Override
    public ResponseResult read(ReadBehaviorDto dto) {
        //数据校验
        if (dto==null||dto.getArticleId()==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        if (BaseContext.getCurrentId()==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.NEED_LOGIN);
        }
        //数据处理
        String key = BehaviorConstants.READ+BaseContext.getCurrentId()+"_"+dto.getArticleId();
        cacheService.set(key,JSON.toJSONString(dto));
        MessageDto messageDto = MessageDto.builder()
                .count(1)
                .type(2)
                .apArticleId(dto.getArticleId().toString())
                .build();
        kafkaTemplate.send(KafkaConstants.MSG, JSON.toJSONString(messageDto));
        //业务逻辑
        return ResponseResult.okResult(200,"记录用户阅读习惯");
    }

    /**
     * 用户不喜欢文章
     *
     * @param dto
     * @return
     */
    @Override
    public ResponseResult un_likes(UnLikesBehaviorDto dto) {
        //参数判断
        if (dto==null||dto.getArticleId()==null||dto.getType()==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        if (BaseContext.getCurrentId()==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.NEED_LOGIN);
        }
        //数据处理
        String key = BehaviorConstants.UN_LIKE+BaseContext.getCurrentId()+"_"+dto.getArticleId();
        //逻辑处理
        if (dto.getType()==0){
            cacheService.set(key,JSON.toJSONString(dto));
        }else {
            cacheService.delete(key);
        }
//        MessageDto.builder()
//                        .apArticleId(dto.getArticleId().toString())
//                                .type()
//        kafkaTemplate.send(KafkaConstants.MSG,JSON.toJSONString(dto));
        return ResponseResult.okResult(200,"记录用户不喜欢文章");
    }


}
